//
//  FQDCalendarIcon.m
//  FakeQDaily
//
//  Created by mewxiv on 9/12/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDCalendarIcon.h"

#import "Masonry.h"

@interface FQDCalendarIcon ()

@property (nonatomic, strong) UIView *dateBgView;
@property (nonatomic, strong) UILabel *monthLabel;
@property (nonatomic, strong) UILabel *dayLabel;
@property (nonatomic, strong) UIImageView *emergencyImageView;

@end

@implementation FQDCalendarIcon

+ (instancetype)calendarIconWithDate:(NSDate *)date
{
    FQDCalendarIcon *calIcon = [[[self class] alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [calIcon showDate:date];
    return calIcon;
}

+ (instancetype)emergencyCalendarIcon
{
    FQDCalendarIcon *calIcon = [[[self class] alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [calIcon showEmergency];
    return calIcon;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI
{
    
    UIView *dateBgView = [[UIView alloc] init];
    dateBgView.backgroundColor = [UIColor clearColor];
    dateBgView.layer.borderWidth = 0.5f;
    dateBgView.layer.borderColor = [RGB(222, 222, 222) CGColor];
    dateBgView.layer.shadowOffset = CGSizeMake(-2, 2);
    dateBgView.layer.cornerRadius = 5.0f;
    dateBgView.layer.masksToBounds = YES;
    dateBgView.opaque = YES;
    [self addSubview:dateBgView];
    self.dateBgView = dateBgView;
    
    WeakSelf;
    [dateBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];

    UILabel *monthLabel = [[UILabel alloc] init];
    monthLabel.font = FQDRegularFontWithSize(8.0f);
    monthLabel.textColor = RGB(115, 115, 115);
    monthLabel.backgroundColor = RGB(240, 240, 240);
    monthLabel.textAlignment = NSTextAlignmentCenter;
    [dateBgView addSubview:monthLabel];
    self.monthLabel = monthLabel;
    
    [monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(dateBgView);
    }];
    
    UIView *dateSepartor = [[UIView alloc] init];
    dateSepartor.backgroundColor = RGB(222, 222, 222);
    [dateBgView addSubview:dateSepartor];
    
    [dateSepartor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(weakSelf);
        make.bottom.equalTo(monthLabel);
        make.height.equalTo(@0.5);
    }];
    
    UILabel *dayLabel = [[UILabel alloc] init];
    dayLabel.font = FQDRegularFontWithSize(12.0f);
    dayLabel.textColor = RGB(115, 115, 115);
    dayLabel.textAlignment = NSTextAlignmentCenter;
    [dateBgView addSubview:dayLabel];
    self.dayLabel = dayLabel;
    
    [dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(monthLabel.mas_bottom);
        make.left.right.and.bottom.equalTo(weakSelf);
    }];
    
    UIImage *emergencyImage = [UIImage imageNamed:@"composite_flash"];
    UIImageView *emergencyImageView = [[UIImageView alloc] initWithImage:emergencyImage];
    [self addSubview:emergencyImageView];
    self.emergencyImageView = emergencyImageView;
    
    [emergencyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    self.dateBgView.hidden = YES;
    self.emergencyImageView.hidden = NO;
}

- (void)showDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.dateFormat = @"MMM d";
    NSString *dateString = [dateFormatter stringFromDate:date];
    NSString *monthString = [dateString substringToIndex:3];
    NSString *dayString = [dateString substringFromIndex:4];
    
    self.monthLabel.text = monthString;
    self.dayLabel.text = dayString;
    
    self.dateBgView.hidden = NO;
    self.emergencyImageView.hidden = YES;
}

- (void)showEmergency
{
    self.dateBgView.hidden = YES;
    self.emergencyImageView.hidden = NO;
}

@end
